package com.ebelter.seneo.ui.fragments.infos;


import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView;

import com.ebelter.btcomlib.common.IOrder;
import com.ebelter.btcomlib.models.db.products.seneo.SeneoUserCurrentMeasureRecord;
import com.ebelter.btcomlib.models.db.products.seneo.SeneoUserinfos;
import com.ebelter.btcomlib.utils.NumUtils;
import com.ebelter.btcomlib.utils.StringUtils;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.seneo.R;
import com.ebelter.seneo.contants.IUserContant;
import com.ebelter.seneo.ui.fragments.BaseFragment;
import com.ebelter.seneo.ui.fragments.main.Info_Fragment;
import com.ebelter.seneo.ui.views.LevelView;
import com.ebelter.seneo.utils.SeneoUtils;


//填写密码
public class Info_BMR_Fragment extends BaseFragment  implements Info_Fragment.IInfoUpdateInterface {

    public static final String TAG = "Info_BMR_Fragment";
    TextView infofly_value_tv;
    TextView infofly_valueunit_tv;
    TextView infofly_tip1_tv;
    LevelView info_levelview;

    @Override
    protected int setLayoutResouceId() {
        return R.layout.info_fly1;
    }

    @Override
    protected void initView() {
        FV();
    }

    private void FV() {
        infofly_value_tv=findViewById(R.id.infofly_value_tv);
        infofly_valueunit_tv=findViewById(R.id.infofly_valueunit_tv);
        infofly_tip1_tv=findViewById(R.id.infofly_tip1_tv);
        info_levelview=findViewById(R.id.info_levelview);
    }


    @Override
    protected void initData(Bundle arguments) {

        String tip=getString(R.string.str_Basal_metabolic_rate_introduce);
        infofly_tip1_tv.setText(tip);

        ViewUtils.setTextViewStr(infofly_valueunit_tv,"kcal");


        //执行了这一句 下面的UpdateCurrentMesureInfo接口会回调display方法
        if (getParentFragment()!=null){
            ((Info_Fragment)getParentFragment()).getCurrentDisplayInfo(TAG);
        }
    }



    @Override
    public void UpdateCurrentMesureInfo(SeneoUserinfos currentUserinfos, SeneoUserCurrentMeasureRecord currentMeasureRecord) {
        displayBmr(currentUserinfos,currentMeasureRecord);
    }


    float[] middle = new float[]{18.5f,24.0f};
    final int[] colors = new int[]{
            Color.parseColor("#49D8F1"),
            Color.parseColor("#54BF4B"),
            Color.parseColor("#EFB847")
    };
    String[] buttoms=new String[]{
            StringUtils.getResStr(R.string.piandi),
            StringUtils.getResStr(R.string.biaozhun),
            StringUtils.getResStr(R.string.piangao)};

    private void displayBmr(SeneoUserinfos currentUserinfos, SeneoUserCurrentMeasureRecord currentMeasureRecord) {
        if (info_levelview==null||currentUserinfos==null)return;

        initMiddleValue(currentUserinfos);//初始化middle值 因为middle值受性别年龄的影响

        info_levelview.setDisInfo(middle,colors,buttoms);

        info_levelview.setMinMaxValuse(0,2000);
        if (currentMeasureRecord!=null){
            if (currentMeasureRecord.bmr!=0){
                info_levelview.setDrawWujiaoxin((true));
                ViewUtils.setTextViewStr(infofly_value_tv,(int)currentMeasureRecord.bmr+"");
            }else if (currentMeasureRecord.bmr == 0){
                info_levelview.setDrawWujiaoxin((false));
                ViewUtils.setTextViewStr(infofly_value_tv, IUserContant.IDIS.Non);
            }
            info_levelview.displayValue(currentMeasureRecord.bmr);
        }else {
            info_levelview.setDrawWujiaoxin((false));
            ViewUtils.setTextViewStr(infofly_value_tv, IUserContant.IDIS.Non);
            info_levelview.displayValue(0);
        }

    }

    private void initMiddleValue(SeneoUserinfos currentUserinfos) {
        int year = SeneoUtils.getBirthdayIntValue(currentUserinfos.birthday,0,1991);
        int age = SeneoUtils.getAgeByBirthday(year);
        int value;
        if (currentUserinfos.sex==0){
            if (age>69){
                value=1220;
            }else if (age>=50&&age<=69){
                value=1350;
            }else if (age>=30&&age<=49){
                value=1500;
            }else {
                value=1550;
            }

        }else {
            if (age>69){
                value=1010;
            }else if (age>=50&&age<=69){
                value=1110;
            }else if (age>=30&&age<=49){
                value=1170;
            }else {
                value=1210;
            }
        }

        float min = value*0.9f;
        float max = value*1.1f;

        middle[0] = NumUtils.numBaoLiuWei0(min);
        middle[1] = NumUtils.numBaoLiuWei0(max);
    }
}
